package display.screens {
	
	import data.ApartmentBasic;
	
	import display.screens.layout.*;
	import display.scrollableWindow.ScrollableWindow;
	
	import events.FloorEvent;
	import events.LoadingEvent;
	import events.ScreenEvent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.ilovecode.display.FloorButton;
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.FloorScreen;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * Screen displays floor views and apartment views.
	 * 
	 * @author Vjekoslav Ratkajec
	 */
	public class LayoutScreen extends FloorScreen {
		
		//{ region Stage elements
		
		//Floor content
		public var floorsContent:LayoutFloors;
		//Apartment content
		public var apartmentsContent:LayoutApartment;
		
		//Apartment floor 2 button switch
		protected var _floor2Button:FloorButton;
		//Apartment floor 3 button switch
		protected var _floor3Button:FloorButton;
		//Apartment floor 4 button switch
		protected var _floor4Button:FloorButton;
		//Apartment floor 5 button switch
		protected var _floor5Button:FloorButton;
		//Apartment floor 6 button switch
		protected var _floor6Button:FloorButton;
		//Apartment floor 7 button switch
		protected var _floor7Button:FloorButton;
		
		//} endregion
		
		//{ region Constructor and intialization
		
		/**
		 * Create new instance of Layout screen.
		 */
		public function LayoutScreen() 
		{
			super(ScreenName.LAYOUT, "layoutScreen");
		}
		
		override protected function construct():void
		{
			super.construct();
			
			/*for (var i:uint = 2; i < 8; i++) {
				this["_floor" + i + "Button"] = FloorButton(this.getChildByName("floor" + i + "Button"));
				FloorButton(this["_floor" + i + "Button"]).id = i;
				_floorButtons.push(this["_floor" + i + "Button"]);
			}*/
		}
		
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			floorsContent.addEventListener(FloorEvent.APARTMENT_CLICK, floorsContent_ApartmentSelected);
			floorsContent.addEventListener(LoadingEvent.SHOW_PROGRESS_ICON, content_Loading);
			floorsContent.addEventListener(LoadingEvent.HIDE_PROGRESS_ICON, content_Loading);
			apartmentsContent.addEventListener(LoadingEvent.SHOW_PROGRESS_ICON, content_Loading);
			apartmentsContent.addEventListener(LoadingEvent.HIDE_PROGRESS_ICON, content_Loading);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			floorsContent.removeEventListener(FloorEvent.APARTMENT_CLICK, floorsContent_ApartmentSelected);
			floorsContent.removeEventListener(LoadingEvent.SHOW_PROGRESS_ICON, content_Loading);
			floorsContent.removeEventListener(LoadingEvent.HIDE_PROGRESS_ICON, content_Loading);
			apartmentsContent.removeEventListener(LoadingEvent.SHOW_PROGRESS_ICON, content_Loading);
			apartmentsContent.removeEventListener(LoadingEvent.HIDE_PROGRESS_ICON, content_Loading);
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			initTextFloorButton(2);
			initTextFloorButton(3);
			initTextFloorButton(4);
			initTextFloorButton(5);
			initTextFloorButton(6);
			initTextFloorButton(7);
		}
		
		//} endregion
		
		//{ region Handlers
		
		private function content_Loading(event:LoadingEvent):void {
			this.dispatchEvent(event.clone());
		}
		
		/**
		 * Show apartment.
		 * 
		 * @param	event	Click on apartment on floor.
		 * @private
		 */
		private function floorsContent_ApartmentSelected(event:FloorEvent):void {
			var apt:ApartmentBasic = ApartmentBasic(event.result);
			if (apt != null) {
				showApartment(apt.id);
			} 
		}
		
		//} endregion
		
		//{ region Public
		
		override public function showFloor(floorNumber:int):void {
			floorsContent.showFloor(floorNumber);
			switchView("floors");
		}
		
		public function showApartment(apartmentID:int, floorNumber:int = -99):void {
			apartmentsContent.showApartment(apartmentID);
			switchView("apartments");
		}
		
		//} endregion
		
		//{ region Helpers
		
		/**
		 * Switch between floor and apartment view.
		 * 
		 * @param	mode	Desired view mode.
		 */
		private function switchView(mode:String):void {
			if (mode == "floors") {
				floorsContent.visible = true;
				apartmentsContent.visible = false;
			} else if (mode == "apartments") {
				floorsContent.visible = false;
				apartmentsContent.visible = true;
			} else {
				throw new Error("Unknown view in layout");
			}
		}
		
		//} endregion
		
	}
	
}